package com.example.elmmerchants.Product_Management;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import java.io.ByteArrayOutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Create_Product_Page extends AppCompatActivity {

    EditText product_name, set_product_price, set_inventory, weight, min_purchase;
    LinearLayout product_image, not_set_image, create, specification_view, attributes_item;
    ImageView show_image;
    RelativeLayout set_scan_code, set_how_to_sold, setting_specification, set_product_attributes;
    TextView code, which_category, has_set_specification, how_many_attributes;
    Switch is_open_auto_re_inventory, is_single_order_without_delivery;

    boolean is_upload_image = false, is_this_bar_code = false, is_finish_find = false, is_finish_upload = false;
    String name = "";
    Double price;
    int weight_number;
    byte[] imageBytes, product_pic;
    Uri image1;
    String email, category_text, temp = "", choose_which_category = "";
    Map<String, Integer> category = new HashMap<>();
    int choose_how_many_category = 0, how_many_specification = 0, get_how_many_attributes = 0;
    ArrayList<String> specification = new ArrayList<>();
    ArrayList<String> attributes = new ArrayList<>();
    AlertDialog dialog;
    String open_hour = "", close_hour = "", open_hour_2 = "", close_hour_2 = "", sold_day = "";

    private final ActivityResultLauncher<Intent> id_card_activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult o) {
            if (o.getResultCode() == RESULT_OK) {
                if (o.getData() != null) {
                    image1 = o.getData().getData();

                    try {
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), image1);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                        imageBytes = baos.toByteArray();
                        is_upload_image = true;

                        show_image.setVisibility(View.VISIBLE);
                        not_set_image.setVisibility(View.GONE);
                        show_image.setImageBitmap(bitmap);

                    } catch (Exception e) {
                    }
                }
            }
            else {
                Toast.makeText(Create_Product_Page.this, "请选择图片", Toast.LENGTH_SHORT).show();
            }
        }
    });

    private class Upload_Data_Thread extends Thread {
        @Override
        public void run() {
            is_finish_upload = false;
            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    Blob blobUpload = connection.createBlob();
                    blobUpload.setBytes(1, imageBytes);
                    String sql = "insert into `elm`.`" + email + "_merchant_product` (inventory, name, picture, is_single_order_without_delivery, product_price, is_auto_return_inventory, weight, description, barcode, product_state, category_1, category_2, category_3, category_4, category_5, sold, specification_name, specification_data, attributes_name, attributes_label, sold_start_hour, sold_end_hour, sold_start_hour_2, sold_end_hour_2, sold_day, min_purchase) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, set_inventory.getText().toString());
                    preparedStatement.setString(2, product_name.getText().toString());
                    preparedStatement.setBlob(3, blobUpload);
                    if (is_single_order_without_delivery.isChecked()) {
                        preparedStatement.setInt(4, 1);
                    }
                    else {
                        preparedStatement.setInt(4, 0);
                    }
                    preparedStatement.setDouble(5, Double.parseDouble(set_product_price.getText().toString()));
                    if (is_open_auto_re_inventory.isChecked()) {
                        preparedStatement.setInt(6, 1);
                    }
                    else {
                        preparedStatement.setInt(6, 0);
                    }
                    preparedStatement.setInt(7, Integer.parseInt(weight.getText().toString()));
                    preparedStatement.setString(8, "");
                    preparedStatement.setString(9, code.getText().toString());
                    preparedStatement.setInt(10, 0);
                    int i = 11;

                    for (String key : category.keySet()) {
                        if (category.get(key) == 1) {
                            preparedStatement.setString(i, key);
                            i += 1;
                        }
                    }
                    for (int j = i; j <= 15; j++) {
                        preparedStatement.setString(j, "");
                    }
                    preparedStatement.setInt(16, 0);
                    preparedStatement.setString(17, "");
                    preparedStatement.setString(18, "");
                    preparedStatement.setString(19, "");
                    preparedStatement.setString(20, "");
                    preparedStatement.setString(21, open_hour);
                    preparedStatement.setString(22, close_hour);
                    preparedStatement.setString(23, open_hour_2);
                    preparedStatement.setString(24, close_hour_2);
                    preparedStatement.setString(25, sold_day);
                    preparedStatement.setInt(26, Integer.parseInt(min_purchase.getText().toString()));
                    preparedStatement.executeUpdate();

                    if (!is_this_bar_code && !code.getText().toString().equals("扫码可快速获取商品信息")) {
                        String sql2 = "insert into `elm`.`bar_code_data` (bar_code, name, price, pic, weight) VALUES (?, ?, ?, ?, ?)";
                        PreparedStatement preparedStatement2 = connection.prepareStatement(sql2);
                        preparedStatement2.setString(1, code.getText().toString());
                        preparedStatement2.setString(2, product_name.getText().toString());
                        preparedStatement2.setDouble(3, Double.parseDouble(set_product_price.getText().toString()));
                        preparedStatement2.setBlob(4, blobUpload);
                        preparedStatement2.setInt(5, Integer.parseInt(weight.getText().toString()));
                        preparedStatement2.executeUpdate();
                    }

                    if (how_many_specification > 0) {
                        String specification_name = "";
                        String specification_data = "";
                        for (int j = 0; j < specification.size(); j += 6) {
                            specification_name += specification.get(j);
                            specification_name += ",";
                            specification_data += specification.get(j + 1);
                            specification_data += ",";
                            specification_data += specification.get(j + 2);
                            specification_data += ",";
                            specification_data += specification.get(j + 4);
                            specification_data += ",";
                            specification_data += specification.get(j + 5);
                            specification_data += ",";
                            specification_data += specification.get(j + 3);
                            specification_data += ",";
                            specification_data += "规格数据已添加";
                            specification_data += ",";
                        }

                        String sql2 = "update `elm`.`" + email + "_merchant_product` set specification_name = ?, specification_data = ? where name = ?";
                        PreparedStatement preparedStatement3 = connection.prepareStatement(sql2);
                        preparedStatement3.setString(1, specification_name);
                        preparedStatement3.setString(2, specification_data);
                        preparedStatement3.setString(3, product_name.getText().toString());
                        preparedStatement3.executeUpdate();
                    }

                    if (get_how_many_attributes > 0) {
                        int k = 0;
                        String attributes_name = "";
                        String attributes_label = "";
                        for (int j = 0; j < attributes.size(); j++) {
                            if (k == 0) {
                                attributes_name += attributes.get(j);
                                attributes_name += ",";
                                k = 1;
                            }
                            else if (attributes.get(j).equals("属性添加完毕")) {
                                attributes_label += "属性添加完毕";
                                attributes_label += ",";
                                k = 0;
                            }
                            else {
                                attributes_label += attributes.get(j);
                                attributes_label += ",";
                            }
                        }
                        String sql3 = "update `elm`.`" + email + "_merchant_product` set attributes_name = ?, attributes_label = ? where name = ?";
                        PreparedStatement preparedStatement2 = connection.prepareStatement(sql3);
                        preparedStatement2.setString(1, attributes_name);
                        preparedStatement2.setString(2, attributes_label);
                        preparedStatement2.setString(3, product_name.getText().toString());
                        preparedStatement2.executeUpdate();
                    }

                    is_finish_upload = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_finish_upload) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    Intent intent = new Intent(Create_Product_Page.this, Shop_Management_Page.class);
                    setResult(1, intent);
                    finish();
                }
            });
        }
    }

    private class Find_BarCode_Thread extends Thread {

        IntentResult intentResult;

        public Find_BarCode_Thread(IntentResult intentResult) {
            this.intentResult = intentResult;
        }

        @Override
        public void run() {
            is_finish_find = false;
            is_this_bar_code = false;
            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM bar_code_data WHERE bar_code = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, intentResult.getContents());
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        name = set.getString("name");
                        price = set.getDouble("price");
                        weight_number = set.getInt("weight");
                        product_pic = set.getBytes("pic");
                        is_this_bar_code = true;
                    }
                    is_finish_find = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_finish_find) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    if (is_this_bar_code) {
                        code.setText(intentResult.getContents());
                        product_name.setText(name);
                        set_product_price.setText(String.valueOf(price));
                        weight.setText(String.valueOf(weight_number));
                        Bitmap bitmap = BitmapFactory.decodeByteArray(product_pic, 0, product_pic.length);
                        show_image.setImageBitmap(bitmap);
                    }
                    else {
                        code.setText(intentResult.getContents());
                    }
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_product_page);

        email = getIntent().getStringExtra("email");
        category_text = getIntent().getStringExtra("category");
        open_hour = getIntent().getStringExtra("open_hour");
        close_hour = getIntent().getStringExtra("close_hour");
        open_hour_2 = getIntent().getStringExtra("open_hour_2");
        close_hour_2 = getIntent().getStringExtra("close_hour_2");
        sold_day = getIntent().getStringExtra("sold_day");

        product_name = findViewById(R.id.product_name);
        product_image = findViewById(R.id.product_image);
        not_set_image = findViewById(R.id.not_set_image);
        show_image = findViewById(R.id.show_image);
        set_product_price = findViewById(R.id.set_product_price);
        set_inventory = findViewById(R.id.set_inventory);
        weight = findViewById(R.id.weight);
        set_scan_code = findViewById(R.id.set_scan_code);
        code = findViewById(R.id.code);
        create = findViewById(R.id.create);
        set_how_to_sold = findViewById(R.id.set_category);
        which_category = findViewById(R.id.which_category);
        setting_specification = findViewById(R.id.setting_specification);
        has_set_specification = findViewById(R.id.has_set_specification);
        specification_view = findViewById(R.id.specification_view);
        set_product_attributes = findViewById(R.id.set_product_attributes);
        attributes_item = findViewById(R.id.attributes_item);
        how_many_attributes = findViewById(R.id.how_many_attributes);
        is_open_auto_re_inventory = findViewById(R.id.is_open_auto_re_inventory);
        min_purchase = findViewById(R.id.min_purchase);
        is_single_order_without_delivery = findViewById(R.id.is_single_order_without_delivery);

        for (int i = 0; i < category_text.length(); i++) {
            if (category_text.charAt(i) == ',') {
                if (!temp.equals("全部")) {
                    category.put(temp, 0);
                }
                temp = "";
            }
            else {
                temp += category_text.charAt(i);
            }
        }

        product_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                id_card_activityResultLauncher.launch(intent);
            }
        });

        set_scan_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentIntegrator intentIntegrator = new IntentIntegrator((Activity) view.getContext());
                intentIntegrator.setPrompt("扫描条形码");
                intentIntegrator.setOrientationLocked(true);
                intentIntegrator.initiateScan();
            }
        });

        set_how_to_sold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                choose_how_many_category = 0;
                Dialog dialog1 = new Dialog(view.getContext());
                dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog1.setContentView(R.layout.set_category_bottom_dialog);
                LinearLayout category_item = dialog1.findViewById(R.id.category_item);
                TextView how_many_category = dialog1.findViewById(R.id.how_many_category);
                LinearLayout ok_button = dialog1.findViewById(R.id.ok_button);
                TextView button_text = dialog1.findViewById(R.id.button_text);
                EditText search = dialog1.findViewById(R.id.search);
                ImageView close = dialog1.findViewById(R.id.close);
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog1.dismiss();
                    }
                });

                search.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        category_item.removeAllViews();
                        if (search.getText().length() == 0) {
                            for (String key : category.keySet()) {
                                View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.choose_category_view, null);
                                CheckBox item = view1.findViewById(R.id.item);
                                item.setText(key);
                                if (category.get(key) == 1) {
                                    item.setChecked(true);
                                }
                                else {
                                    item.setChecked(false);
                                }
                                item.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if (item.isChecked() && choose_how_many_category >= 5) {
                                            item.setChecked(false);
                                            Toast.makeText(Create_Product_Page.this, "最多只可选择5个", Toast.LENGTH_SHORT).show();
                                        }
                                        else if (item.isChecked()) {
                                            choose_how_many_category += 1;
                                            category.replace(key, 1);
                                        }
                                        else {
                                            choose_how_many_category -= 1;
                                            category.replace(key, 0);
                                        }
                                        how_many_category.setText(String.valueOf(choose_how_many_category));
                                        if (choose_how_many_category >= 1) {
                                            ok_button.setBackgroundResource(R.drawable.border_yellow_5dp);
                                            button_text.setTextColor(Color.BLACK);
                                        }
                                        else if (choose_how_many_category == 0) {
                                            ok_button.setBackgroundResource(R.drawable.border_light_gray);
                                            button_text.setTextColor(Color.WHITE);
                                        }
                                    }
                                });
                                category_item.addView(view1);
                            }
                        }
                        else {
                            for (String key : category.keySet()) {
                                if (key.indexOf(search.getText().toString()) != -1) {
                                    View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.choose_category_view, null);
                                    CheckBox item = view1.findViewById(R.id.item);
                                    item.setText(key);
                                    if (category.get(key) == 1) {
                                        item.setChecked(true);
                                    }
                                    else {
                                        item.setChecked(false);
                                    }
                                    item.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            if (item.isChecked() && choose_how_many_category >= 5) {
                                                item.setChecked(false);
                                                Toast.makeText(Create_Product_Page.this, "最多只可选择5个", Toast.LENGTH_SHORT).show();
                                            }
                                            else if (item.isChecked()) {
                                                choose_how_many_category += 1;
                                                category.replace(key, 1);
                                            }
                                            else {
                                                choose_how_many_category -= 1;
                                                category.replace(key, 0);
                                            }
                                            how_many_category.setText(String.valueOf(choose_how_many_category));
                                            if (choose_how_many_category >= 1) {
                                                ok_button.setBackgroundResource(R.drawable.border_yellow_5dp);
                                                button_text.setTextColor(Color.BLACK);
                                            }
                                            else if (choose_how_many_category == 0) {
                                                ok_button.setBackgroundResource(R.drawable.border_light_gray);
                                                button_text.setTextColor(Color.WHITE);
                                            }
                                        }
                                    });
                                    category_item.addView(view1);
                                }
                            }
                        }
                    }
                });

                for (String key : category.keySet()) {
                    View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.choose_category_view, null);
                    CheckBox item = view1.findViewById(R.id.item);
                    item.setText(key);
                    if (category.get(key) == 1) {
                        item.setChecked(true);
                        choose_how_many_category += 1;
                    }
                    else {
                        item.setChecked(false);
                    }
                    item.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (item.isChecked() && choose_how_many_category >= 5) {
                                item.setChecked(false);
                                Toast.makeText(Create_Product_Page.this, "最多只可选择5个", Toast.LENGTH_SHORT).show();
                            }
                            else if (item.isChecked()) {
                                choose_how_many_category += 1;
                                category.replace(key, 1);
                            }
                            else {
                                choose_how_many_category -= 1;
                                category.replace(key, 0);
                            }
                            how_many_category.setText(String.valueOf(choose_how_many_category));
                            if (choose_how_many_category >= 1) {
                                ok_button.setBackgroundResource(R.drawable.border_yellow_5dp);
                                button_text.setTextColor(Color.BLACK);
                            }
                            else if (choose_how_many_category == 0) {
                                ok_button.setBackgroundResource(R.drawable.border_light_gray);
                                button_text.setTextColor(Color.WHITE);
                            }
                        }
                    });
                    category_item.addView(view1);
                }
                how_many_category.setText(String.valueOf(choose_how_many_category));
                if (choose_how_many_category >= 1) {
                    ok_button.setBackgroundResource(R.drawable.border_yellow_5dp);
                    button_text.setTextColor(Color.BLACK);
                }
                else if (choose_how_many_category == 0) {
                    ok_button.setBackgroundResource(R.drawable.border_light_gray);
                    button_text.setTextColor(Color.WHITE);
                }
                ok_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (choose_how_many_category > 0) {
                            choose_which_category = "";
                            for (String key : category.keySet()) {
                                if (category.get(key) == 1) {
                                    choose_which_category += key;
                                    choose_which_category += ",";
                                }
                            }
                            which_category.setText(choose_which_category);
                            dialog1.dismiss();
                        }
                        else {
                            Toast.makeText(Create_Product_Page.this, "请选择分类", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                dialog1.show();
                dialog1.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog1.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                dialog1.getWindow().setGravity(Gravity.BOTTOM);
            }
        });

        create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (product_name.getText().toString().isEmpty() || !is_upload_image || set_product_price.getText().toString().isEmpty() || set_inventory.getText().toString().isEmpty() || weight.getText().toString().isEmpty() || min_purchase.getText().toString().isEmpty()) {
                    Toast.makeText(Create_Product_Page.this, "请输入所有数据", Toast.LENGTH_SHORT).show();
                }
                else {
                    set_dialog();
                    Upload_Data_Thread uploadDataThread = new Upload_Data_Thread();
                    uploadDataThread.start();
                }
            }
        });

        setting_specification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Create_Product_Page.this, Setting_Specification_Page.class);
                startActivityForResult(intent, 1);
            }
        });

        set_product_attributes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Create_Product_Page.this, Product_Attributes_Page.class);
                startActivityForResult(intent, 2);
            }
        });
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater1 = LayoutInflater.from(this);
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            if (intentResult.getContents() == null) {
                Toast.makeText(getBaseContext(), "未添加条形码", Toast.LENGTH_SHORT).show();
            } else {
                set_dialog();
                Find_BarCode_Thread findBarCodeThread = new Find_BarCode_Thread(intentResult);
                findBarCodeThread.start();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }

        switch (requestCode) {
            case 1: {
                if (data != null) {
                    specification_view.removeAllViews();
                    how_many_specification = 0;
                    specification = data.getStringArrayListExtra("specification_data");
                    for (int i = 0; i < specification.size(); i += 6) {
                        how_many_specification += 1;
                        View view = LayoutInflater.from(this).inflate(R.layout.show_specification_card_view, null);
                        TextView specification_name = view.findViewById(R.id.specification_name);
                        specification_name.setText(specification.get(i));
                        TextView specification_price = view.findViewById(R.id.specification_price);
                        specification_price.setText(specification.get(i + 1));
                        TextView specification_inventory = view.findViewById(R.id.specification_inventory);
                        specification_inventory.setText(specification.get(i + 2));
                        TextView specification_weight = view.findViewById(R.id.specification_weight);
                        specification_weight.setText(specification.get(i + 3));
                        specification_view.addView(view);
                    }
                    has_set_specification.setText("共" + how_many_specification + "规格");
                }
                else {
                    has_set_specification.setText("请设置");
                    specification_view.removeAllViews();
                }
                break;
            }

            case 2: {
                if (data != null) {
                    get_how_many_attributes = 0;
                    attributes_item.removeAllViews();
                    attributes = data.getStringArrayListExtra("attributes");
                    int j = 0;
                    View view = LayoutInflater.from(this).inflate(R.layout.show_attributes_card_view, null);
                    LinearLayout label_item = view.findViewById(R.id.label_item);
                    for (int i = 0; i < attributes.size(); i++) {
                        if (j == 0) {
                            TextView attributes_name = view.findViewById(R.id.attributes_name);
                            attributes_name.setText(attributes.get(i));
                            j = 1;
                            get_how_many_attributes += 1;
                        }
                        else if (attributes.get(i).equals("属性添加完毕")) {
                            attributes_item.addView(view);
                            view = LayoutInflater.from(this).inflate(R.layout.show_attributes_card_view, null);
                            label_item = view.findViewById(R.id.label_item);
                            j = 0;
                        }
                        else {
                            View view1 = LayoutInflater.from(this).inflate(R.layout.show_label_card_view, null);
                            TextView label_name = view1.findViewById(R.id.label_name);
                            label_name.setText(attributes.get(i));
                            label_item.addView(view1);
                        }
                    }
                    how_many_attributes.setText("共" + get_how_many_attributes + "属性");
                }
                else {
                    how_many_attributes.setText("请设置");
                    attributes_item.removeAllViews();
                }
                break;
            }
        }
    }
}